package simbot.example.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import simbot.example.entity.GroupQu;
import simbot.example.entity.Param;
import simbot.example.mapper.GroupQuMapper;
import simbot.example.mapper.ParamMapper;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author renshuting
 * @date 2023/5/5 16:45
 */
public class QuFuUtil {
    public static Param getParam(String param) throws IOException {
        //获取mybatis
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 获取mapper
        ParamMapper paramMapper = sqlSession.getMapper(ParamMapper.class);
        Param param1 = paramMapper.selectById(param);
        sqlSession.close();
        return param1;
    }

    public static GroupQu getGroup(long groupId) throws IOException {
        //获取mybatis
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2. 获取SqlSession对象，用它来执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3. 获取mapper
        GroupQuMapper groupQuMapper = sqlSession.getMapper(GroupQuMapper.class);
        GroupQu groupQu = groupQuMapper.selectById(groupId);
        sqlSession.close();
        return groupQu;
    }
}
